{% extends 'base.html' %}

{% block title %}选择小区{% endblock %}
{% block content %}
 <header class='demos-header'>
      <h1 class="demos-title">选择小区</h1>
    </header>
    <div class="weui-cells__title">小区列表</div>
    <div class="weui-cells weui-cells_radio">
    	{% if communities %}

    	{% for cmnt in communities %}
	      <label class="weui-cell weui-check__label" for="cmnt{{cmnt.id}}">
	        <div class="weui-cell__bd">
	          <p>{{cmnt.cmnt_name}}</p>
	        </div>
	        <div class="weui-cell__ft">
	          <input type="radio" class="weui-check" name="community" id="cmnt{{cmnt.id}}" value="{{cmnt.id}}" {% if g.user.cmnt_id == cmnt.id %} checked="checked" {% endif %}>
	          <span class="weui-icon-checked"></span>
	        </div>
	      </label>
      	{% endfor %}
      {% endif %}

      <a href="javascript:void(0);" class="weui-cell weui-cell_link">
        <div class="weui-cell__bd">添加更多</div>
      </a>
    </div>

    <div class="weui-btn-area">
      <a class="weui-btn weui-btn_primary" href="javascript:chooseCommunity()" id="showTooltips">确定</a>
    </div>

{% endblock %}

{% block jsblock %}
function chooseCommunity() {
	var item = $("input:radio[name='community']:checked").val();
	if({{g.user.cmnt_id}} != item) {
		postData = {cmnt_id:item};

		$.ajax({
		  type: 'POST',
		  url: '/auth/selectCommunity',
		  data: postData,
		  success: function(data) {
		  			 	if(data.success) {
		  			 		$.toast("操作成功", function() {
					           $(location).attr('href', '/auth/profile');
					        });
			  			}else {
			  				$.toast("操作失败请重试", "forbidden");
			  			}
		           },
		  error: function(msg) {
		  			$.toast("操作失败请重试", "forbidden");
				}
		});


	}else{
		$.toast("操作成功", function() {
           $(location).attr('href', '/auth/profile');
        });
	}
}
{% endblock %}